/*
 * @Author: 杨上漂
 * @Date: 2024-10-12 17:18:04
 * @LastEditors: 杨上漂
 * @LastEditTime: 2024-10-16 10:35:07
 * @FilePath: \gwlc\assets\scripts\EnemyMgr.ts
 * @Description: ng
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */

import { _decorator, Component, instantiate, JsonAsset, Node, Prefab, v2 } from 'cc';
import { Enemy } from './Enemy';
import { gameMgr } from './gameMgr';
import { Dictionary } from '../utils/Dictionary';
import { randomNum } from '../utils/utils';
import { PoolMgr } from '../PoolMgr';
import { StartSceneGameMgr } from '../startScene/StartSceneGameMgr';
import { AudioMgr } from '../utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('EnemyMgr')
export class EnemyMgr extends Component {

    @property(JsonAsset)
    enemyConfi:JsonAsset;
    enemyRoot: Node;

    static ins: EnemyMgr = null;

    enemyNodeDic: Dictionary<number, Node> = new Dictionary();
    enemyId = 0;//敌人唯一id标识,id多少意味着生成了几个怪物

    protected onLoad(): void {
        EnemyMgr.ins = this;

        this.enemyRoot = this.node.getChildByName("bg").getChildByName("enemyRoot")
    }

    protected start(): void {
    }

    createEnemy(num, enemyTypeEnum) {
        for (let i = 0; i < num; i++) {
            let enemyPre = PoolMgr.getIns().poolGet("enemy");
            let randowPosition = v2(Math.round(Math.random() * 660 - 330), StartSceneGameMgr.ins.enemyGenerateY)
            enemyPre.setPosition(randowPosition.x, randowPosition.y, 0);
            enemyPre.setParent(this.enemyRoot);
            enemyPre.getComponent(Enemy).init(this.enemyId,enemyTypeEnum);
            this.enemyNodeDic.set(this.enemyId, enemyPre)
            this.enemyId += 1;
            enemyPre.setSiblingIndex(0);

        }
    }
    
    /**
     * @description: 得到场上最近一个敌人node
     * @return {*}
     */
    getNearlyExistOneEnemyNode() {
        let maxNearlyNode;
        if (this.enemyNodeDic.lenght <= 0) {
            return null
        }
        
        this.enemyNodeDic.getValueList().forEach((ele) => {
            if (maxNearlyNode == null) {
                maxNearlyNode = ele
            }
            else {
                if (maxNearlyNode.position.y > ele.position.y) {
                    maxNearlyNode = ele
                }
            }
        })

        return maxNearlyNode;
    }

     /**
     * @description: 得到场上最近一个敌人node,正在遭受龙卷风不选
     * @return {*}
     */
     getNearlyExistOneEnemyNodeNottTornado() {
        let maxNearlyNode;
        if (this.enemyNodeDic.lenght <= 0) {
            return null
        }
        
        this.enemyNodeDic.getValueList().forEach((ele) => {
            if (ele.getComponent(Enemy).isSufferTornadoIng==true) {
                return;
            }
            if (maxNearlyNode == null) {
                maxNearlyNode = ele
            }
            else {
                if (maxNearlyNode.position.y > ele.position.y) {
                    maxNearlyNode = ele
                }
            }
        })

        return maxNearlyNode;
    }

    /**
     * @description: 得到场上最近的四个敌人node,如果场上没敌人返回bull，如果只有两个敌人，返回两个敌人的数组
     * @return {*}
     */
    getNearlyExistFourEnemyNode() {
        let maxNearlyNode;
        let maxNearly2Node;//第二近
        let maxNearly3Node;
        let maxNearly4Node;
        if (this.enemyNodeDic.lenght <= 0) {
            return null
        }
        
        this.enemyNodeDic.getValueList().forEach((ele) => {
            if (maxNearlyNode == null) {
                maxNearlyNode = ele
            }
            else {
                if (maxNearlyNode.position.y > ele.position.y) {
                    maxNearlyNode = ele
                }
            }
        })

        for (let i = 0; i < this.enemyNodeDic.lenght; i++) {
                if (this.enemyNodeDic.getValueList()[i]==maxNearlyNode) {
                    continue;
                }
                if (maxNearly2Node == null) {
                    maxNearly2Node = this.enemyNodeDic.getValueList()[i];
                }
                else {
                    if (maxNearly2Node.position.y > this.enemyNodeDic.getValueList()[i].position.y) {
                        maxNearly2Node = this.enemyNodeDic.getValueList()[i]
                    }
                }
           
        }

        for (let i = 0; i < this.enemyNodeDic.lenght; i++) {

                if (this.enemyNodeDic.getValueList()[i] == maxNearlyNode || this.enemyNodeDic.getValueList()[i] == maxNearly2Node) {
                    continue;
                }
                if (maxNearly3Node == null) {
                    maxNearly3Node = this.enemyNodeDic.getValueList()[i];
                }
                else {
                    if (maxNearly3Node.position.y > this.enemyNodeDic.getValueList()[i].position.y) {
                        maxNearly3Node = this.enemyNodeDic.getValueList()[i]
                    }
                }
        }

        for (let i = 0; i < this.enemyNodeDic.lenght; i++) {

            if (this.enemyNodeDic.getValueList()[i] == maxNearlyNode || this.enemyNodeDic.getValueList()[i] == maxNearly2Node|| this.enemyNodeDic.getValueList()[i] == maxNearly3Node) {
                continue;
            }
            if (maxNearly4Node == null) {
                maxNearly4Node = this.enemyNodeDic.getValueList()[i];
            }
            else {
                if (maxNearly4Node.position.y > this.enemyNodeDic.getValueList()[i].position.y) {
                    maxNearly4Node = this.enemyNodeDic.getValueList()[i]
                }
            }
    }

        let resArr=[maxNearlyNode];
        if (maxNearly2Node) {
            resArr.push(maxNearly2Node)
        }
        if (maxNearly3Node) {
            resArr.push(maxNearly3Node)
        }
        if (maxNearly4Node) {
            resArr.push(maxNearly4Node)
        }
        return resArr;
    }

     /**
     * @description: 获得战场随机一个敌人node
     * @return {*}
     */
     getAnyExistOneEnemyNode() {
        let enemyNode;
        if (this.enemyNodeDic.lenght <= 0) {
            return null
        }
        
        enemyNode=this.enemyNodeDic.getValueList()[randomNum(0,this.enemyNodeDic.lenght-1)]
      

        return enemyNode;
    }

    /**
     * @description: 获得战场随机四个敌人node
     * @return {*}
     */
    getAnyExistFourEnemyNode() {
        let ArrCount;//输出几个敌人node
        let enemyNodeArr=[];
        if (this.enemyNodeDic.lenght <= 0) {
            return null
        }
        else if (this.enemyNodeDic.lenght ==1) {
            ArrCount=1;
        }
        else if (this.enemyNodeDic.lenght ==2) {
            ArrCount=2;

        }
        else if (this.enemyNodeDic.lenght == 3) {
            ArrCount=3;

        }
        else if (this.enemyNodeDic.lenght >= 4) {
            ArrCount=4;
        }

        while (enemyNodeArr.length<ArrCount) {
            let needAddNode = this.enemyNodeDic.getValueList()[randomNum(0, this.enemyNodeDic.lenght - 1)];
            if (enemyNodeArr.indexOf(needAddNode) == -1) {
                enemyNodeArr.push(needAddNode)
            } 
        }

        return enemyNodeArr;
    }

    //开启僵尸潮
    openZombieTide(){
        gameMgr.ins.createWidowTip("尸潮来袭")
        AudioMgr.ins.playOneShot("audio/openZombieTide",0.6);
        gameMgr.ins.isOpenZombieTide=true;
    }



    
}


